import QtQuick 2.0
import QtMultimedia 5.0

Item {
    id: parent;
    width: 1670; height: 820
    property string selected : "#FF274E13"
    property string unselected :  "#FF38761D"
    property string theme : "green"
    property string refrigatorImage : "images/FridgeDisabled.png";
    property string microOvenImage
    property string stoveImage
    property string fanImage
    property string upArrowImage
    property string downArrowImage
    property string stopTemperatureImage
    property string timeUpArrowImage
    property string timeDownArrowImage
    property string temperatureUnit : "F"
    property string counter : "0"

    function setTheme(){
        if(parent.theme == "green"){
            parent.refrigatorImage = "images/FridgeEnabled.png";
            parent.microOvenImage = "images/microOvenEnabled.png";
            parent.downArrowImage = "images/downArrow.png";
            parent.upArrowImage = "images/upArrow.png";
            parent.fanImage = "images/fanEnabled.png";
            parent.timeDownArrowImage = "images/timeDownArrow.png";
            parent.timeUpArrowImage = "images/timeUpArrow.png";
            parent.stopTemperatureImage = "images/stopTemperature.png";
            parent.stoveImage = "images/stoveEnabled.png";
        }else if(parent.theme == "blue"){
            parent.refrigatorImage = "images/FridgeEnabledBlue.png";
            parent.microOvenImage = "images/microOvenEnabledBlue.png";
            parent.downArrowImage = "images/downArrowBlue.png";
            parent.upArrowImage = "images/upArrowBlue.png";
            parent.fanImage = "images/fanEnabledBlue.png";
            parent.timeDownArrowImage = "images/timeDownArrowBlue.png";
            parent.timeUpArrowImage = "images/timeUpArrowBlue.png";
            parent.stopTemperatureImage = "images/stopTemperatureBlue.png";
            parent.stoveImage = "images/stoveEnabledBlue.png";
        }else if(parent.theme == "red"){
            parent.refrigatorImage = "images/FridgeEnabledRed.png";
            parent.microOvenImage = "images/microOvenEnabledRed.png";
            parent.downArrowImage = "images/downArrowRed.png";
            parent.upArrowImage = "images/upArrowRed.png";
            parent.fanImage = "images/fanEnabledRed.png";
            parent.timeDownArrowImage = "images/timeDownArrowRed.png";
            parent.timeUpArrowImage = "images/timeUpArrowRed.png";
            parent.stopTemperatureImage = "images/stopTemperatureRed.png";
            parent.stoveImage = "images/stoveEnabledRed.png";
        }
    }

    function checkThemeStatus(){
        if(fridgeStatus.text == "on"){
            if(parent.theme == "green")
                refrigerator.source = "images/FridgeEnabled.png";
            else if(parent.theme == "blue")
                refrigerator.source = "images/FridgeEnabledBlue.png";
            else if(parent.theme == "red")
                refrigerator.source = "images/FridgeEnabledRed.png";
        }if(stoveStatus.text == "on"){
            if(parent.theme == "green")
                stove.source = "images/microOvenEnabled.png";
            else if(parent.theme == "blue")
                stove.source = "images/microOvenEnabledBlue.png";
            else if(parent.theme == "red")
                stove.source = "images/microOvenEnabledRed.png";
        }if(microOvenStatus.text == "on"){
            if(parent.theme == "green")
                microOven.source = "images/stoveEnabled.png";
            else if(parent.theme == "blue")
                microOven.source = "images/stoveEnabledBlue.png";
            else if(parent.theme == "red")
                microOven.source = "images/stoveEnabledRed.png";
        }if(fanStatus.text == "on"){
            if(parent.theme == "green")
                fanPlaceHolder.source = "images/fanEnabled.png";
            else if(parent.theme == "blue")
                fanPlaceHolder.source = "images/fanEnabledBlue.png";
            else if(parent.theme == "red")
                fanPlaceHolder.source = "images/fanEnabledRed.png";
        }if(temperatureController.text == "on"){
            if(parent.theme == "green")
                stopTemperature.source = "images/stopTemperature.png";
            else if(parent.theme == "blue")
                stopTemperature.source = "images/stopTemperatureBlue.png";
            else if(parent.theme == "red")
                stopTemperature.source = "images/stopTemperatureBlue.png";
        }
    }

    function convertTemperature(){
           if(parent.temperatureUnit == "F"){
                temperature.text = Math.round(parseInt(temperature.text)*(9/5)+32);
                stoveTempNo.text = Math.round(parseInt(stoveTempNo.text)*(9/5)+32);
                fridgeTempNo.text = Math.round(parseInt(fridgeTempNo.text)*(9/5)+32);
           }else if(parent.temperatureUnit == "C"){
                temperature.text = Math.round((parseInt(temperature.text)-32)*(5/9));
                stoveTempNo.text = Math.round((parseInt(stoveTempNo.text)-32)*(5/9));
                fridgeTempNo.text = Math.round((parseInt(fridgeTempNo.text)-32)*(5/9));
           }
    }
    
    function initializeTemp() {
        if(parent.temperatureUnit == "F"){
            temperature.text = "80";
            stoveTempNo.text = "350";
            fridgeTempNo.text = "38";
        }
        else if(parent.temperatureUnit == "C"){
            temperature.text = Math.round((80-32)*(5/9));
            stoveTempNo.text = Math.round((350-32)*(5/9));
            fridgeTempNo.text = Math.round((38-32)*(5/9));
        }
    }

    Image {
        id: refrigerator
        x:200
        y:50
        width: 150
        height: 200
        source: "images/FridgeDisabled.png"
        visible : true;
        MouseArea{
                       x: 1
                       y: 1
                       anchors.rightMargin: 0
                       anchors.bottomMargin: 0
                       anchors.leftMargin: 1
                       anchors.topMargin: 1
                       anchors.fill: refrigerator
                       onClicked: {
                           if(fridgeStatus.text == "off"){
                               refrigerator.source = parent.refrigatorImage;
                               fridgeTemp.visible = true;
                               fridgeSafeMode.visible = true;
                               fridgeFreezerStatus.visible = true;
                               fridgeTempNo.visible = true;
                               fridgeUnit.visible = true;
                               fridgeStatus.text = "on";
                           }else if(fridgeStatus.text = "on"){
                               refrigerator.source = "images/FridgeDisabled.PNG"
                               fridgeTemp.visible = false;
                               fridgeSafeMode.visible = false;
                               fridgeFreezerStatus.visible = false;
                               fridgeTempNo.visible = false;
                               fridgeUnit.visible = false;
                               fridgeStatus.text = "off";
                           }

           }
        }
    }

    Text {
        id : fridgeStatus
        text:"off"
        font.family: "Verdana"
        font.pointSize: 12
        color: "#000000"
        x: 400
        y:75
        visible: false
    }

    Text {
        id : fridgeTemp
        text:"Temperature : "
        font.family: "Verdana"
        font.pointSize: 12
        color: "#000000"
        x: 550
        y:75
        visible: false
    }

    Text {
        id : fridgeTempNo
        text:"48"
        font.family: "Verdana"
        font.pointSize: 12
        color: "#000000"
        x: 665
        y:75
        visible: false
    }

    Text {
        id: fridgeUnit
        anchors.top: parent.top
        anchors.topMargin: 75
        anchors.left: parent.left
        anchors.leftMargin:685
        text: "\u00B0"+parent.temperatureUnit
        color: "#FF000000"
        font { family: "Verdana"; pointSize: 12; bold: false; }
        visible: false
    }

    Text {
        id : fridgeFreezerStatus
        text:"Status : OFF "
        font.family: "Verdana"
        font.pointSize: 12
        color: "#000000"
        x: 550
        y: 125
        visible: false
    }

    Text {
        id : fridgeSafeMode
        text:"Safe Mode : ON "
        font.family: "Verdana"
        font.pointSize: 12
        color: "#000000"
        x: 550
        y: 175
        visible: false
    }

    Image {
        id: microOven
        x: 200
        y: 330
        width: 150
        height: 200
        source: "images/stoveDisabled.png"
        visible : true;
        MouseArea{
                       x: 1
                       y: 1
                       anchors.rightMargin: 0
                       anchors.bottomMargin: 0
                       anchors.leftMargin: 1
                       anchors.topMargin: 1
                       anchors.fill: microOven
                       onClicked: {
                           if(microOvenStatus.text == "off"){
                               microOven.source =  parent.stoveImage;
                               microTemp.visible = true;
                               microTime.visible = true;
                               microStatus.visible = true;
                               microOvenStatus.text = "on";
                           }else if(microOvenStatus.text = "on"){
                               microOven.source = "images/stoveDisabled.png"
                               microTemp.visible = false;
                               microTime.visible = false;
                               microStatus.visible = false;
                               microOvenStatus.text = "off";
                           }
           }
        }
    }

    Text {
        id : microOvenStatus
        text:"off"
        font.family: "Verdana"
        font.pointSize: 12
        color: "#000000"
        x: 400
        y:75
        visible: false
    }

    Text {
        id : microTemp
        text:"No. of Burners ON : 3"
        font.family: "Verdana"
        font.pointSize: 12
        color: "#000000"
        x: 550
        y: 375
        visible: false
    }

    Text {
        id : microTime
        text:"Flame Status : HIGH"
        font.family: "Verdana"
        font.pointSize: 12
        color: "#000000"
        x: 550
        y: 425
        visible: false
    }

    Text {
        id : microStatus
        text:"Gas Level : MEDIUM"
        font.family: "Verdana"
        font.pointSize: 12
        color: "#000000"
        x: 550
        y: 475
        visible: false
    }




    Image {
        id: stove
        x: 200
        y: 600
        width: 150
        height: 100
        source: "images/microOvenDisabled.png"
        visible : true;
        MouseArea{
                       x: 1
                       y: 1
                       anchors.rightMargin: 0
                       anchors.bottomMargin: 0
                       anchors.leftMargin: 1
                       anchors.topMargin: 1
                       anchors.fill: stove
                       onClicked: {
                           if(stoveStatus.text == "off"){
                               stove.source = parent.microOvenImage;
                               burnerStatus.visible = true;
                               flameStatus.visible = true;
                               gasStatus.visible = true;
                               stoveTempNo.visible = true;
                               stoveUnit.visible = true;
                               stoveStatus.text = "on";
                           }else if(stoveStatus.text == "on"){
                               stove.source = "images/microOvenDisabled.png"
                               burnerStatus.visible = false;
                               flameStatus.visible = false;
                               gasStatus.visible = false;
                               stoveTempNo.visible = false;
                               stoveUnit.visible = false;
                               stoveStatus.text = "off";
                           }
           }
        }
    }

    Text {
        id : stoveStatus
        text:"off"
        font.family: "Verdana"
        font.pointSize: 12
        color: "#000000"
        x: 500
        y: 125
        visible: false
    }

    Text {
        id : burnerStatus
        text:"Temperature: "
        font.family: "Verdana"
        font.pointSize: 12
        color: "#000000"
        x: 550
        y: 585
        visible: false
    }

    Text {
        id : stoveTempNo
        text:"37"
        font.family: "Verdana"
        font.pointSize: 12
        color: "#000000"
        x: 665
        y: 585
        visible: false
    }

    Text {
        id: stoveUnit
        anchors.top: parent.top
        anchors.topMargin: 585
        anchors.left: parent.left
        anchors.leftMargin:685
        text: "\u00B0"+parent.temperatureUnit
        color: "#FF000000"
        font { family: "Verdana"; pointSize: 12; bold: false; }
        visible: false
    }

    Text {
        id : flameStatus
        text:"Time : 09:25"
        font.family: "Verdana"
        font.pointSize: 12
        color: "#000000"
        x: 550
        y: 635
        visible: false
    }

    Text {
        id : gasStatus
        text:"Status : PAUSE"
        font.family: "Verdana"
        font.pointSize: 12
        color: "#000000"
        x: 550
        y: 685
        visible: false
    }

    Image {
        id: upArrow
        x: 900
        y: 600
        width: 150
        height: 100
        source: parent.upArrowImage
        visible : true;
        MouseArea{
                       x: 1
                       y: 1
                       anchors.rightMargin: 0
                       anchors.bottomMargin: 0
                       anchors.leftMargin: 1
                       anchors.topMargin: 1
                       anchors.fill: upArrow
                       onClicked: {
                        temperature.text = parseInt(temperature.text)+1;
           }
        }
    }

    Image {
        id: downArrow
        x: 1100
        y: 600
        width: 150
        height: 100
        source: parent.downArrowImage
        visible : true;
        MouseArea{
                       x: 1
                       y: 1
                       anchors.rightMargin: 0
                       anchors.bottomMargin: 0
                       anchors.leftMargin: 1
                       anchors.topMargin: 1
                       anchors.fill: downArrow
                       onClicked: {
                           temperature.text = parseInt(temperature.text)-1;

           }
        }
    }

    Text {
        id : temperature
        text:"80"
        font.family: "Verdana"
        font.pointSize: 50
        font.bold: true
        color: "#000000"
        x: 1120
        y: 475
        visible: true
    }

    Text {
        id: tempUnit
        anchors.top: parent.top
        anchors.topMargin: 485
        anchors.right: parent.right
        anchors.rightMargin:400
        text: "\u00B0"+parent.temperatureUnit
        color: "#FF000000"
        font { family: "Verdana"; pointSize: 30; bold: true; }
    }

    Image {
        id: stopTemperature
        x: 1100
        y: 325
        width: 150
        height: 100
        source: parent.stopTemperatureImage
        visible : true;
        MouseArea{
                       x: 1
                       y: 1
                       anchors.rightMargin: 0
                       anchors.bottomMargin: 0
                       anchors.leftMargin: 1
                       anchors.topMargin: 1
                       anchors.fill: stopTemperature
                       onClicked: {
                           if(temperatureController.text == "off"){
                               upArrow.visible = true;
                               downArrow.visible = true;
                               temperature.visible = true;
                               tempUnit.visible = true;
                               fanPlaceHolder.visible = true;
                               temperatureController.text = "on";
                           }else if(temperatureController.text == "on"){
                               upArrow.visible = false;
                               downArrow.visible = false;
                               temperature.visible = false;
                               tempUnit.visible = false;
                               fanPlaceHolder.visible = false;
                               temperatureController.text = "off";
                           }
           }
        }
    }

    Text {
        id : stopText
        text:"Control ON/OFF"
        font.family: "Verdana"
        font.pointSize: 20
        font.bold: true
        color: "#000000"
        x: 1080
        y: 270
        visible: true
    }



    Text {
        id: seconds
        x: 1260
        y: 60
        width: 100
        height: 100
        color: "#0b1c7c"
        font.pixelSize: 61
        text: "00"
        font.family: "Verdana"
        font.bold: true
    }

    Image {
        id: secondsUpArrow
        x: 1340
        y: 75
        width: 50
        height: 30
        source: parent.timeUpArrowImage
        visible : true;
        MouseArea{
                       x: 1
                       y: 1
                       anchors.rightMargin: 0
                       anchors.bottomMargin: 0
                       anchors.leftMargin: 1
                       anchors.topMargin: 1
                       anchors.fill: secondsUpArrow
                       onClicked: {
                           if((parseInt(seconds.text,10)+1)>59){
                               seconds.text = "59";
                           }else{
                               if((parseInt(seconds.text,10)+1)<10)
                                   seconds.text = "0"+(parseInt(seconds.text,10)+1);
                               else
                                   seconds.text = (parseInt(seconds.text,10)+1);

                           }


           }
        }
    }

    Image {
        id: secondsDownArrow
        x: 1340
        y: 95
        width: 50
        height: 30
        source: parent.timeDownArrowImage
        visible : true;
        MouseArea{
                       x: 1
                       y: 1
                       anchors.rightMargin: 0
                       anchors.bottomMargin: 0
                       anchors.leftMargin: 1
                       anchors.topMargin: 1
                       anchors.fill: secondsDownArrow
                       onClicked: {
                           if((parseInt(seconds.text,10)-1)<0){
                               seconds.text = "00";
                           }else{

                               if((parseInt(seconds.text,10)-1)<10)
                                   seconds.text = "0"+(parseInt(seconds.text,10)-1);
                               else
                                   seconds.text = (parseInt(seconds.text,10)-1);

                           }

           }
        }
    }


    Text {
        id: secondsColon
        x: 1240
        y: 55
        width: 5
        height: 20
        color: "#0b1c7c"
        font.pixelSize: 61
        text: ":"
        font.family: "Verdana"
        font.bold: true
    }

    Text {
        id: minutes
        x: 1130
        y: 60
        width: 100
        height: 100
        color: "#0b1c7c"
        font.pixelSize: 61
        text: "00"
        font.family: "Verdana"
        font.bold: true
    }

    Image {
        id: minutesUpArrow
        x: 1205
        y: 75
        width: 50
        height: 30
        source: parent.timeUpArrowImage
        visible : true;
        MouseArea{
                       x: 1
                       y: 1
                       anchors.rightMargin: 0
                       anchors.bottomMargin: 0
                       anchors.leftMargin: 1
                       anchors.topMargin: 1
                       anchors.fill: minutesUpArrow
                       onClicked: {
                           if((parseInt(minutes.text,10)+1)>59){
                               minutes.text = "59";
                           }else{
                               if((parseInt(minutes.text,10)+1)<10)
                                   minutes.text = "0"+(parseInt(minutes.text,10)+1);
                               else
                                   minutes.text = (parseInt(minutes.text,10)+1);

                           }


           }
        }
    }

    Image {
        id: minutesDownArrow
        x: 1205
        y: 95
        width: 50
        height: 30
        source: parent.timeDownArrowImage
        visible : true;
        MouseArea{
                       x: 1
                       y: 1
                       anchors.rightMargin: 0
                       anchors.bottomMargin: 0
                       anchors.leftMargin: 1
                       anchors.topMargin: 1
                       anchors.fill: minutesDownArrow
                       onClicked: {
                           if((parseInt(minutes.text,10)-1)<0){
                               minutes.text = "00";
                           }else{

                               if((parseInt(minutes.text,10)-1)<10)
                                   minutes.text = "0"+(parseInt(minutes.text,10)-1);
                               else
                                   minutes.text = (parseInt(minutes.text,10)-1);

                           }

           }
        }
    }


    Text {
        id: minutesColon
        x: 1110
        y: 55
        width: 5
        height: 20
        color: "#0b1c7c"
        font.pixelSize: 61
        text: ":"
        font.family: "Verdana"
        font.bold: true
    }


    Text {
        id: hours
        x: 995
        y: 60
        width: 100
        height: 100
        color: "#0b1c7c"
        font.pixelSize: 61
        text: "00"
        font.family: "Verdana"
        font.bold: true

    }

    Image {
        id: hoursUpArrow
        x: 1075
        y: 75
        width: 50
        height: 30
        source: parent.timeUpArrowImage
        visible : true;
        MouseArea{
                       x: 1
                       y: 1
                       anchors.rightMargin: 0
                       anchors.bottomMargin: 0
                       anchors.leftMargin: 1
                       anchors.topMargin: 1
                       anchors.fill: hoursUpArrow
                       onClicked: {
                           if((parseInt(hours.text,10)+1)>59){
                               hours.text = "59";
                           }else{
                               if((parseInt(hours.text,10)+1)<10)
                                   hours.text = "0"+(parseInt(hours.text,10)+1);
                               else
                                   hours.text = (parseInt(hours.text,10)+1);

                           }


           }
        }
    }

    Image {
        id: hoursDownArrow
        x: 1075
        y: 95
        width: 50
        height: 30
        source: parent.timeDownArrowImage
        visible : true;
        MouseArea{
                       x: 1
                       y: 1
                       anchors.rightMargin: 0
                       anchors.bottomMargin: 0
                       anchors.leftMargin: 1
                       anchors.topMargin: 1
                       anchors.fill: hoursDownArrow
                       onClicked: {
                           if((parseInt(hours.text,10)-1)<0){
                               hours.text = "00";
                           }else{

                               if((parseInt(hours.text,10)-1)<10)
                                   hours.text = "0"+(parseInt(hours.text,10)-1);
                               else
                                   hours.text = (parseInt(hours.text,10)-1);

                           }

           }
        }
    }

    AppButton {
        id: startButton
        text : "Start"
        x: 1000
        y : 150
        selected: parent.selected
        unselected: parent.unselected
        visible: true
        onClicked: {
            stopWatch.running = true;
        }
     }

    AppButton {
        id: stopButton
        text : "Stop"
        selected: parent.selected
        unselected: parent.unselected
        x: 1150
        y : 150
        visible: true
        onClicked: {
            stopWatch.running = false;
            beepSound.running = false;
        }
     }

    AppButton {
        id: resetButton
        text : "Reset"
        selected: parent.selected
        unselected: parent.unselected
        x: 1300
        y : 150
        visible: true
        onClicked: {
            stopWatch.running = false;
            beepSound.running = false;
            hours.text = "00";
            minutes.text = "00";
            seconds.text = "00";
        }
     }

    Audio {
        id: stopWatchBeep
        source: "sounds/scannerBeep.mp3"
    }


    Timer {
            id : stopWatch
            interval: 1000; running: false; repeat: true
            onTriggered: {

                var hoursInt = parseInt(hours.text,10);
                var minutesInt = parseInt(minutes.text,10);//currentTime.getMinutes()
                var secondsInt = parseInt(seconds.text,10);
                var trigger = 0;
                //console.log(secondsInt);

                if(secondsInt == 0){
                    if(minutesInt >0){
                        minutesInt = minutesInt - 1;
                        minutes.text = minutesInt;
                        seconds.text = "59";
                        if(minutesInt < 10)
                            minutes.text = "0"+minutesInt;
                        else
                            minutes.text = minutesInt;

                    }else if(hoursInt >0){
                        hoursInt = hoursInt - 1;
                        if(hoursInt==0)
                            hours.text = "00";
                        else
                            hours.text = hoursInt;
                        minutes.text = "59";
                        seconds.text = "59";
                    }else{
                        seconds.text = "00";
                        minutes.text = "00";
                        hours.text = "00";
                        hoursInt = 0;
                        minutesInt = 0;
                        secondsInt = 0;
                        trigger = 1;
                    }
                }else{
                    secondsInt = secondsInt - 1;
                    if(secondsInt <10)
                        seconds.text = "0"+secondsInt;
                    else
                        seconds.text = secondsInt;
                }
                if(trigger === 1){
                    beepSound.running = true;
                    stopWatch.running = false;
                }

            }
        }

    Timer {
            id : beepSound
            interval: 1000; running: false; repeat: true
            onTriggered: {

                stopWatchBeep.play();
                if(parseInt(parent.counter) == 5){
                    parent.counter = "0";
                    running = false;
                }
                parent.counter = parseInt(parent.counter)+1;
                console.log(parent.counter);

            }
    }

    Text {
        id : temperatureController
        text:"off"
        font.family: "Verdana"
        font.pointSize: 12
        color: "#000000"
        x: 400
        y: 125
        visible: false
    }

    /*Image {
        id: changeTemperatureUnit
        x: 1000
        y: 400
        width: 150
        height: 100
        source: "images/fEnabled.png"
        visible : true;
        MouseArea{
                       x: 1
                       y: 1
                       anchors.rightMargin: 0
                       anchors.bottomMargin: 0
                       anchors.leftMargin: 1
                       anchors.topMargin: 1
                       anchors.fill: changeTemperatureUnit
                       onClicked: {
                           if(temperatureUnit.text == "f"){
                            temperature.text = Math.round((parseInt(temperature.text)-32)*(5/9));
                            changeTemperatureUnit.source = "images/cEnabled.png";
                            temperatureUnit.text = "c";
                           }else if(temperatureUnit.text == "c"){
                               temperature.text = Math.round(parseInt(temperature.text)*(9/5)+32);
                               changeTemperatureUnit.source = "images/fEnabled.png";
                               temperatureUnit.text = "f";
                           }

           }
        }
    }

    Text {
        id : temperatureUnit
        text:"f"
        font.family: "Helvetica"
        font.pointSize: 12
        color: "#000000"
        x: 400
        y:75
        visible: false
    }*/

    Image {
        id: fanPlaceHolder
        x: 1300
        y: 600
        width: 150
        height: 100
        source: parent.fanImage
        visible : true;
        MouseArea{
                       x: 1
                       y: 1
                       anchors.rightMargin: 0
                       anchors.bottomMargin: 0
                       anchors.leftMargin: 1
                       anchors.topMargin: 1
                       anchors.fill: fanPlaceHolder
                       onClicked: {
                           if(fanStatus.text == "off"){
                               fanPlaceHolder.source = "images/fanDisabled.png"
                               fanStatus.text = "on"
                           }else if(fanStatus.text == "on"){
                               fanPlaceHolder.source = parent.fanImage
                               fanStatus.text = "off"
                           }

           }
        }
    }

    Text {
        id : fanStatus
        text:"on"
        font.family: "Verdana"
        font.pointSize: 12
        color: "#000000"
        x: 400
        y:75
        visible: false
    }


}
